<template>
  <div class="search-result">
    <!-- 导航栏 -->
    <van-nav-bar :title="$route.query.keyword+'的搜索结果'"
                 left-arrow
                 fixed
                 @click-left="$router.back()" />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list class="article-list"
              v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              @load="onLoad">
      <van-cell v-for="item in list"
                :key="item.art_id.toString()"
                :title="item.title"
                @click="$router.push('/article/'+item.art_id.toString())" />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { reqGetResult } from '@/api/search'
export default {
  name: 'SearchResult',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    }
  },
  methods: {
    async onLoad () {
      const data = await reqGetResult(this.$route.query.keyword, this.page)
      // console.log(data)
      const results = data.data.results // 返回的数据
      this.list.push(...results)
      // 将page自增
      this.page++
      // 手动将loading设置为false
      this.loading = false
      // 设置没有更多数据了 设置finished  一定设置加载结束
      if (results.length === 0) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-result {
  .article-list {
    margin-top: 39px;
  }
}
</style>
